package main

import (
	"fmt"
	"strconv"
)

func main() {
	//var x X
	//x.desc()

	/*u := User{}
	u.setName("徐悦")
	println("u address:", u.ToString())
	u.setAge(22)
	println("u 's age is :", u.age)*/

	/*var b A = "123456"
	b.print()*/

	//printByte()

	//test(red)
	//test(blue)

	//convertStr()

	//add()

	//printRWX()

	//ddd()

	//sss()

	//leftShift()

	SecondLevelPointer()

}

type X int

func (x *X) inc() {
	println("x 原来的值：", x)
	//*x++
	aa := *x
	println("x的值为:", aa)
	println("aa 地址：", &aa)
}

func (a *X) desc() {
	/*var aa int = 45
	ptr := &aa
	fmt.Printf("x的值:%d", *ptr)*/

	m := map[string]string{"name": "zhangsan", "age": "18"}
	ptrm := &m
	age := (*ptrm)["age"]
	print("age is : ", age)
}

type User struct {
	name string
	age  int
}

func (user *User) setAge(age int) {
	user.age = age
}
func (user *User) setName(name string) {
	user.name = name
}

func (u User) ToString() string {
	return fmt.Sprintf("User{Name: %s,Age: %d}", u.name, u.age)
}

type A string

func (a *A) print() {
	fmt.Println("a is :", *a)
}

const (
	_  = iota
	KB = 1 << (10 * iota)
	MB
	GB
)

func printByte() {
	println("KB: ", KB)
	println("MB: ", MB)
	println("GB: ", GB)
}

type color byte

const (
	black color = iota
	red
	blue
)

func test(c color) {
	println(c)
}

func convertStr() {
	a, _ := strconv.ParseInt("010101", 2, 64)
	println(a)
	binary := strconv.FormatInt(a, 2)
	println(binary)
}

func add() {
	var x int = 100
	var y int64 = int64(x)
	println(x, y)
}

type flags byte

const (
	read flags = 1 << iota
	write
	exec
	tt = 100
	bb = 1 << iota
	xx
)

func printRWX() {
	println(read, write, exec)
}

func ddd() {
	type data int
	var d data = 10
	var x int = int(d)
	println(x)
}

func sss() {
	a := make(chan int, 2)
	var b chan<- int = a
	var c <-chan int = a
	b <- 10
	b <- 20
	for {
		select {
		case tmp, ok := <-c:
			if !ok {
				fmt.Println("Channel closed")
				return
			}
			fmt.Println(tmp)
		default:
			fmt.Println("No more data")
			close(b)
			close(a)
			return
		}
	}
	b <- 50
	/*
		for tmp := range c {
			println(tmp)
		}*/
	println(<-c)

}

func leftShift() {
	//var b uint8 = 23 //b是有符号int类型变量
	//x := 1 << b      // 无效操作:1<<b(shift count type int,must be unsigned integer)
	//println(x)

	//var s uint = 3
	//b := 1.0 << s // 无效操作:1<<s(shift of type float64)
	//fmt.Printf("%T, %v\n", b, b)

	a := 1
	p := &a
	*p++
	println(a)
}

func SecondLevelPointer() {
	var a int = 8
	var p1 *int = &a
	var p2 **int = &p1
	var p3 ***int = &p2
	fmt.Println(p1, p2, p3)
	fmt.Println(***p3)
}
